package Serverlet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.google.gson.Gson;


import net.sf.json.JSONObject;
import tools.Lolita;

public class Userservlet {
	public  String register(String request) {
        
        Gson gson=new Gson();
        Map map = gson.fromJson(request, Map.class);

        String username=(String) map.get("username");
        String password=(String) map.get("password");
        String str = gson.toJson(map);
        
        JSONObject jsobj=gson.fromJson(str, JSONObject.class);
        try {
			new Lolita().AddLolita("user",jsobj);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return "register success!";
	}
	
	public  String login(String request) {
        
        Gson gson=new Gson();
        Map map = gson.fromJson(request, Map.class);

        String username=(String) map.get("username");
        String password=(String) map.get("password");
        JSONArray jsarr=new JSONArray();
        try {
        	jsarr=new Lolita().GetLolita("user",username,"username");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        for(Object obj:jsarr) {
        	 String user=gson.toJson(obj);
             Map map2 = gson.fromJson(user, Map.class);
             if((map2.get("password").toString()).equals(password))
            	 return map2.get("id").toString();
        }
       
        return "false";
	}
	
}
